<template>
  <el-dialog :visible="showRoleDialog" title="分配角色" @close="close">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="(item,index) in list" :key="index" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handelConfirm">确定</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailByid } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  components: {},
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      list: [],
      roleIds: []
    }
  },
  computed: {},
  watch: {

  },
  async created() {
    const { rows } = await getRoleList({ page: 1, pagesize: 50 })
    this.list = rows
  },
  mounted() {},
  methods: {
    async getUserDetailByid(id) {
      const { roleIds } = await getUserDetailByid(id)
      this.roleIds = roleIds || []
    },
    close() {
      this.roleIds = []
      this.$emit('update:showRoleDialog', false)
    },
    async handelConfirm() {
      await assignRoles({
        id: this.userId,
        roleIds: this.roleIds
      })
      this.$message.success('修改成功')
      this.close()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
